package com.aviccxzx.smarthome.UserModule;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.GosUserModuleBaseActivity;
import com.gizwits.gizwifisdk.api.GizDeviceSharing;
import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.enumration.GizDeviceSharingWay;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizDeviceSharingListener;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import static com.aviccxzx.smarthome.fragment.CamFragment.cams;
import static com.aviccxzx.smarthome.fragment.DeviceListFragment.mDevice;
import static com.aviccxzx.smarthome.fragment.DeviceListFragment.subList;


/**
 * Created by joey on 2016/10/26.
 */
public class DeviceShareAct extends GosUserModuleBaseActivity implements View.OnClickListener {

    ListView device_share_list, cam_share_list;

    List<String> posList = new ArrayList<>();
    List<String> posCamList = new ArrayList<>();//存在设备和摄像机的信息，保证只能选择一个
    ImageView gatelist_down, gatelist_up, camlist_down, camlist_up;
    FriendsAdapter adapter = null;
    CamShareAdapter camadapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(com.aviccxzx.smarthome.R.layout.friends_share_lay_v191);
        } else {

        }
        setView();
        setListView();
    }

    private void setView() {
        ((TextView) findViewById(R.id.title_tv)).setText("设备分享");
        TextView title_right_text = (TextView) findViewById(R.id.title_right_text);
        title_right_text.setVisibility(View.VISIBLE);
        title_right_text.setText("分享记录");
        device_share_list = (ListView) findViewById(R.id.device_share_list);
        cam_share_list = (ListView) findViewById(R.id.cam_share_list);
        gatelist_up = (ImageView) findViewById(R.id.gatelist_up);
        gatelist_down = (ImageView) findViewById(R.id.gatelist_down);
        camlist_up = (ImageView) findViewById(R.id.camlist_up);
        camlist_down = (ImageView) findViewById(R.id.camlist_down);
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        findViewById(R.id.title_right_img).setOnClickListener(this);
        findViewById(R.id.btn_share_auth).setOnClickListener(this);
        findViewById(R.id.show_gatelist).setOnClickListener(this);
        findViewById(R.id.show_camlist).setOnClickListener(this);
        title_right_text.setOnClickListener(this);

        adapter = new FriendsAdapter();
        camadapter = new CamShareAdapter();
        device_share_list.setAdapter(adapter);
        cam_share_list.setAdapter(camadapter);
    }


    List<GizWifiDevice> mysubList = new ArrayList<>();

    private void setListView() {
        mysubList.clear();
        mysubList.addAll(subList);
        mysubList.add(0, mDevice);
        adapter.notifyDataSetChanged();
        camadapter.notifyDataSetChanged();
    }

    Intent intent;
    String deviceId = "";
    String yst = "";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.show_gatelist:
                if (device_share_list.getVisibility() == View.GONE) {
                    device_share_list.setVisibility(View.VISIBLE);
                    gatelist_up.setVisibility(View.VISIBLE);
                    gatelist_down.setVisibility(View.GONE);
                } else {
                    device_share_list.setVisibility(View.GONE);
                    gatelist_up.setVisibility(View.GONE);
                    gatelist_down.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.show_camlist:
                if (cam_share_list.getVisibility() == View.GONE) {
                    cam_share_list.setVisibility(View.VISIBLE);
                    camlist_up.setVisibility(View.VISIBLE);
                    camlist_down.setVisibility(View.GONE);
                } else {
                    cam_share_list.setVisibility(View.GONE);
                    camlist_up.setVisibility(View.GONE);
                    camlist_down.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.title_right_text:
                //分享记录
                intent = new Intent(this, ShareRecordAct.class);
                startActivity(intent);
                break;
            case R.id.btn_share_auth:
                progressDialog.show();
                if (posList.size() > 0) {
                    // 设置设备分享监听
                    GizDeviceSharing.setListener(mListener);
                    // 在设备列表中找到可以分享的设备
                    // 二维码分享设备
                    String token = spf.getString("Token", "");
                    GizDeviceSharing.sharingDevice(token, deviceId, GizDeviceSharingWay.GizDeviceSharingByQRCode, null, null);
                } else {
                    //摄像机的分享
                    Intent intent = new Intent(this, CamShareActivity.class);
                    intent.putExtra("yst", yst);
                    startActivity(intent);
                    finish();
                }
                break;
        }
    }

    GizDeviceSharingListener mListener = new GizDeviceSharingListener() {
        // 实现设备分享的回调
        @Override
        public void didSharingDevice(GizWifiErrorCode result, String deviceID, int sharingID, Bitmap QRCodeImage) {
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                // 分享成功
                intent = new Intent(DeviceShareAct.this, TwobarShareAct.class);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                QRCodeImage.compress(Bitmap.CompressFormat.PNG, 100, baos);
                byte[] bitmapByte = baos.toByteArray();
                intent.putExtra("bitmap", bitmapByte);
                startActivity(intent);
                finish();
            } else {
                // 分享失败
                showToast("分享失败，请联系管理员添加");
            }
            progressDialog.dismiss();
        }
    };


    @Override
    protected void onResume() {
        super.onResume();
    }


    public class FriendsAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mysubList.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = null;


            if (convertView != null) {
                view = convertView;
            } else {
                view = LayoutInflater.from(DeviceShareAct.this).inflate(R.layout.friends_item_lay, parent, false);
            }

            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                holder.arrow = (ImageView) view.findViewById(R.id.arrow);
                holder.choose_circle = (ImageView) view.findViewById(R.id.choose_circle);
                holder.device_name = (TextView) view.findViewById(R.id.device_name);
                view.setTag(holder);
            }

            device_share_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    if (posCamList.size() > 0) {
                        posCamList.clear();
                    }
                    if (posList.contains(position + "")) {
                        posList.remove(position + "");
                    } else {
                        posList.removeAll(posList);
                        posList.add(position + "");
                    }
                    adapter.notifyDataSetChanged();
                    camadapter.notifyDataSetChanged();
                }
            });

            if (posList.contains(position + "")) {
                holder.choose_circle.setImageResource(R.mipmap.checked_circle);
                deviceId = mysubList.get(position).getDid();
            } else {
                holder.choose_circle.setImageResource(R.mipmap.unchecked_circle);
            }
            if (mysubList.get(position) != null) {
                holder.device_name.setText(!TextUtils.isEmpty(mysubList.get(position).getAlias()) ? mysubList.get(position).getAlias() :
                        mysubList.get(position).getProductName());
            }

            return view;
        }

        public class ViewHolder {
            ImageView arrow;
            ImageView choose_circle;
            TextView device_name;
        }
    }

    public class CamShareAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (cams.size() > 0) {
                return cams.size();
            } else {
                return 0;
            }
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = null;


            if (convertView != null) {
                view = convertView;
            } else {
                view = LayoutInflater.from(DeviceShareAct.this).inflate(R.layout.friends_item_lay, parent, false);
            }

            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                holder.arrow = (ImageView) view.findViewById(R.id.arrow);
                holder.choose_circle = (ImageView) view.findViewById(R.id.choose_circle);
                holder.device_name = (TextView) view.findViewById(R.id.device_name);
                view.setTag(holder);
            }

            cam_share_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    if (posList.size() > 0) {
                        posList.clear();
                    }
                    if (posCamList.contains(position + "")) {
                        posCamList.remove(position + "");
                    } else {
                        posCamList.removeAll(posCamList);
                        posCamList.add(position + "");
                    }
                    camadapter.notifyDataSetChanged();
                    adapter.notifyDataSetChanged();
                }
            });

            if (posCamList.contains(position + "")) {
                holder.choose_circle.setImageResource(R.mipmap.checked_circle);
                yst = cams.get(position).getYst();
            } else {
                holder.choose_circle.setImageResource(R.mipmap.unchecked_circle);
            }
            if (cams.get(position) != null) {
                holder.device_name.setText(cams.get(position).getYst());
            }

            return view;
        }

        public class ViewHolder {
            ImageView arrow;
            ImageView choose_circle;
            TextView device_name;
        }
    }
}
